package com.example.book.controller;

import ch.qos.logback.core.util.StringUtil;

import com.example.book.model.BookInfo;
import com.example.book.model.PageRequest;
import com.example.book.model.PageResult;
import com.example.book.model.Result;
import com.example.book.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping(value = "/addBook", produces = "application/json")
    public String addBook(BookInfo bookInfo){
        log.info("添加图书, bookInfo: {}", bookInfo);
        //参数校验
        if (bookInfo==null
                || !StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount()==null
                || bookInfo.getPrice() ==null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus()==null ){
            return "参数校验失败";
        }
        try {
            Integer result = bookService.addBook(bookInfo);
            if (result==1){
                return "";
            }
        }catch (Exception e){
            log.error("添加图书发生异常, e: {}", e);
            return "内部错误, 请联系管理员";
        }
        return "内部错误, 请联系管理员";
    }

    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>> getListByPage(PageRequest pageRequest){

        log.info("请求图书列表, pageRequest:{}", pageRequest);
        //判断用户是否登录
//        if (!StringUtils.hasLength((String)session.getAttribute(Constants.SESSION_LOGIN_USER_NAME))){
//            return Result.unlogin();
//        }
        Result<PageResult<BookInfo>> re = Result.success(bookService.getListByPage(pageRequest));

        return re;
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        log.info("查询图书信息, bookId:{}", bookId);
        if (bookId==null || bookId<1){
            return null;
        }
        return bookService.queryBookById(bookId);
    }

    @RequestMapping(value = "/updateBook", produces = "application/json")
    public String updateBook(BookInfo bookInfo){
        log.info("更新图书信息, bookInfo:{}", bookInfo);
        try {
            Integer reuslt = bookService.updateBook(bookInfo);
            if (reuslt<1){
                return "图书更新失败, 请联系管理员";
            }
            return "";
        }catch (Exception e){
            log.error("图书更新发生异常, e:", e);
            return "图书更新失败, 请联系管理员";
        }
    }

    @RequestMapping(value = "/deleteBook", produces = "application/json")
    public String deleteBook(Integer bookId){
        log.info("删除图书信息, bookId:{}", bookId);
        try {
            Integer reuslt = bookService.deleteBook(bookId);
            if (reuslt<1){
                return "图书删除失败, 请联系管理员";
            }
            return "";
        }catch (Exception e){
            log.error("图书删除发生异常, e:", e);
            return "图书删除失败, 请联系管理员";
        }
    }
    @RequestMapping(value = "/batchDeleteBook", produces = "application/json")
    public String batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书, ids;{}", ids);
        //参数校验自行补充
        Integer result = bookService.batchDeleteBook(ids);
        return result>0 ? "" : "删除失败, 请联系管理员";
    }
}
